<?php

/*
 * Action 3$
 *
 * Look through your discard pile. You may trash a card from your discard pile or hand that is not a Treasure.
 * Gain a card costing up to 3 Coins.
 * When you discard this from play, if you did not buy any cards this turn, trash this and gain a Madman from the Madman pile.
 */

class Hermit extends Card implements iAction, iKingdom, iDarkAges {

    public function __construct() {
        parent::__construct("Hermit", 3);
        $this->undoPlay = false;
        $this->undoBuy = true;
    }

    public function getDescription() {
        return array(
            "Look through your discard pile. You may trash a card from your discard pile or hand that is not a Treasure.",
            "Gain a card costing up to 3 Coins.",
            "When you discard this from play, if you did not buy any cards this turn, trash this and gain a Madman from the Madman pile.");
    }

    public function cleanup(Game $game) {
        $boughtCards = $game->getBoughtCards();
        if (sizeof($boughtCards) == 0) {
            $game->moveCardFromPileToPile($this, PlayArea, Trash);
            $madman = $game->moveCardFromPileToPile("Madman", Madman, DiscardPile);
            ScreenDisplay::addScreenMessage($game, true, "bought nothing so you trash ".  nicePrint($this)." and gain " . nicePrint($madman), " bought nothing so he trashes ".  nicePrint($this)." and gains " . nicePrint($madman));
        }
    }

    public function play(Game $game, $isRealCard) {
        $options = array();
        $discard = $game->getDiscardDeck();
        foreach ($discard as $card) {
            if ($card instanceof iTreasure)
                continue;
            else
                $options[] = $card;
        }
        $trashedCard = false;
        if (sizeof($options) > 0) {
            ScreenDisplay::addScreenMessage($game, true, " look through your discard pile and find " . nicePrint($options));
            $options[] = new Option(1, "None");
            $output = MySocket::GetUserReply($game, Input_ChooseOne, $options, 1, 1, null, "Choose what card to trash, choose None if you want to examine your play hand for possible trash card");
            $answer = $output[0];
            if ($answer != "None") {
                $card = $game->moveCardFromPileToPile($answer, DiscardPile, Trash);
                ScreenDisplay::addScreenMessage($game, true, "trash " . nicePrint($card), "trashes " . ScreenDisplay::$LMSG);
                $trashedCard = true;
            } else {
                //do nothing here
            }
        } else {
            ScreenDisplay::addScreenMessage($game, true, "do not have non-treasure cards in discard deck");
        }
        if (!$trashedCard) {
            $options = array();
            $playHand = $game->getPlayHand();
            foreach ($playHand as $card) {
                if ($card instanceof iTreasure)
                    continue;
                else
                    $options[] = $card;
            }
            $trashedCard = false;
            if (sizeof($options) > 0) {
                ScreenDisplay::addScreenMessage($game, true, " look through your play hand and find " . nicePrint($options));
                $options[] = new Option(1, "None");
                $output = MySocket::GetUserReply($game, Input_ChooseOne, $options, 1, 1, null, "Choose what card to trash");
                $answer = $output[0];
                if ($answer != "None") {
                    $card = $game->moveCardFromPileToPile($answer, PlayHand, Trash);
                    ScreenDisplay::addScreenMessage($game, true, "trash " . nicePrint($card), "trashes " . ScreenDisplay::$LMSG);
                    $trashedCard = true;
                } else {
                    ScreenDisplay::addScreenMessage($game, true, "trash nothing ", "trashes nothing");
                }
            }
        }

        $optional_cards = $game->getBoardCardsByPrice(new Price(0), new Price(3));
        if (sizeof($optional_cards) > 0) {
            $output = MySocket::GetUserReply($game, Input_ChooseOne, $optional_cards, 1, 1, null, "Choose a card to gain");

            $new_card_name = $output[0];
            $new_card = $game->moveCardFromPileToPile($new_card_name, BoardCards, DiscardPile);
            ScreenDisplay::addScreenMessage($game, true, " and gain " . nicePrint($new_card), " and gains " . ScreenDisplay::$LMSG);
        }
		$game->cardActionFinished();
        return true;
    }

    public   function boardSetup(Game $game) {
        $game->addMadman();
        return true;
    }

}

?>